#include "../common/elasticsearch.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志的输出等级");

int main(int argc, char *argv[]) 
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto client = std::make_shared<elasticlient::Client>(std::vector<std::string>{"http://127.0.0.1:9200/"});

    ESIndex index(client, "test_user", "_doc");
    bool ret = index.append("nickname").append("phone", "keyword", "standard", true).create();
    if (ret == false) {
        LOG_INFO("索引创建失败!");
        return -1;
    }

    // 新增
    ret = ESInsert(client, "test_user", "_doc").append("nickname", "张三").append("phone", "15555666777").insert("00001");
    if (ret == false) {
        LOG_INFO("数据插入失败!");
        return -1;
    }

    // // 数据的修改
    ret = ESInsert(client, "test_user", "_doc").append("nickname", "张三").append("phone", "13344445555").insert("00001");
    if (ret == false) {
        LOG_INFO("数据修改失败!");
        return -1;
    }

    Json::Value user = ESSearch(client, "test_user", "_doc").append_should_match("phone.keyword", "13344445555").append_must_not_terms("nickname.keyword", {"1"}).search();
    if (user.empty() || !user.isArray()) {
        LOG_ERROR("结果为空或者结果不是数组类型!");
        return -1;
    }

    int sz = user.size();
    for (int i = 0; i < sz; i++) {
        LOG_INFO("nickname: {}", user[i]["_source"]["nickname"].asString());
    }

    // 数据的删除
    ret = ESRemove(client, "test_user", "_doc").remove("00001");
    if (ret == false) {
        LOG_INFO("数据删除失败!");
        return -1;
    }

    return 0;
}